package com.ali.juejin.ui.activity

import androidx.lifecycle.ViewModelProvider
import com.ali.juejin.R
import com.ali.juejin.base.BaseActivity
import com.ali.juejin.model.main.CategoryModel
import com.ali.juejin.ui.fragment.*
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity: BaseActivity() {

    val categoryModel by lazy { ViewModelProvider(this).get(CategoryModel::class.java) }

    val homeFragment by lazy { HomeFragment() }
    val recommendFragment by lazy { RecommendFragment() }
    val searchFragment by lazy { SearchFragment() }
    val bookFragment by lazy { BookFragment() }
    val accountFragment by lazy { AccountFragment() }

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initView() {



        val fragments = arrayOf(homeFragment,recommendFragment,searchFragment,bookFragment,accountFragment)
        supportFragmentManager.beginTransaction().add(R.id.main_fragments,fragments[0]).commit()

        bottom_navigation_bar.setBarBackgroundColor(R.color.colorWhite).setMode(BottomNavigationBar.MODE_FIXED)
        bottom_navigation_bar.addItem(BottomNavigationItem(R.drawable.home_select,"主页").setInactiveIconResource(R.drawable.home_unselect))
            .addItem(BottomNavigationItem(R.drawable.recommend_select,"沸点").setInactiveIconResource(R.drawable.recommend_unselect))
            .addItem(BottomNavigationItem(R.drawable.search_select,"发现").setInactiveIconResource(R.drawable.search_unselect))
            .addItem(BottomNavigationItem(R.drawable.book_select,"小册").setInactiveIconResource(R.drawable.book_unselect))
            .addItem(BottomNavigationItem(R.drawable.account_select,"我").setInactiveIconResource(R.drawable.account_unselect))
            .setFirstSelectedPosition(0)
            .initialise()

        bottom_navigation_bar.setTabSelectedListener(object:BottomNavigationBar.OnTabSelectedListener{

            override fun onTabReselected(position: Int) {
            }

            override fun onTabUnselected(position: Int) {

            }

            override fun onTabSelected(position: Int) {
                val transaction = supportFragmentManager.beginTransaction()
                if(!supportFragmentManager.fragments.contains(fragments[position])){
                    transaction.add(R.id.main_fragments,fragments[position])
                }
                for(i in fragments){
                    transaction.hide(i)
                }
                transaction.show(fragments[position])
                transaction.commit()
            }

        })
    }

    override fun initData() {
        categoryModel.getCategoryList()
    }

}